﻿var realEdit = new Class({
    //加载数据
    LoadData: function () {
        //初始化装修下拉框
        dataBind.bindOption(38, "#selDecorationSituationKeyId");
        //家电列表ulAppliance
        var dataAppliance;
        var dataFurniture;
        for (var i = 0; i < SysParam.length; i++) {
            //家电
            if (SysParam[i].ParameterType == 36) {
                dataAppliance = SysParam[i].Items;
            }
            if (SysParam[i].ParameterType == 37) {
                dataFurniture = SysParam[i].Items;
            }
        }
        if (dataAppliance) {
            var liObj = '';
            for (var k = 0; k < dataAppliance.length; k++) {
                liObj += '<li><label class="tit">' + dataAppliance[k].ItemText + '</label>';
                liObj += '<input type="hidden" name="ElectricKeyId" value="' + dataAppliance[k].ItemValue + '" />';
                liObj += '<label>数量<input name="ElectricCount" isint="1" class="ipt ipt-short" type="text" maxlength="5"/></label>';
                liObj += '<label>品牌<input name="ElectricBrand"   class="ipt w124" type="text" maxlength="30"/></label></li>';
            }
            $('#ulApplianceValue').before(liObj);
        }
        if (dataFurniture) {
            var liObj2 = '';
            for (var j = 0; j < dataFurniture.length; j++) {
                liObj2 += '<li><label class="tit">' + dataFurniture[j].ItemText + '</label>';
                liObj2 += '<input type="hidden" name="FurnitureKeyId" value="' + dataFurniture[j].ItemValue + '" />';
                liObj2 += '<label>数量<input name="FurnitureCount" isint="1" class="ipt ipt-short" type="text" maxlength="5"/></label>';
                liObj2 += '<label>品牌<input  name="FurnitureBrand" class="ipt w124" type="text" maxlength="30"/></label></li>';
            }
            $('#ulFurnitureValue').before(liObj2);
        }
    },
    //表单验证
    Verify: function () {
        //表单验证
        agencyVerify.nonnegativeIntInput("input[isint=1]", false);
        agencyVerify.notNullInput("#SaveForm", "#txtRealSurveyComment", false, "blur");
        if ($("#uploadRoomTypeDiv").children().length > 1) {
            //验证下拉框不能为空的验证
            agencyVerify.notNullInput("#SaveForm", "#uploadRoomTypeDiv select[isnotnull=1]", true, "change");
            $("#uploadRoomTypeDiv select[isnotnull=1]").change();
        }
        $("#SaveForm").bind("submit", function () {
            var currennum = $("input[name=roomimg]").length;
            if (currennum < $("#hidMinUpate").val()) {
                $.OKDialogAgency('<div class="tc">你上传的室内图不能少于' + $("#hidMinUpate").val() + '张！</div>');
                return false;
            }
            if (currennum > $("#hidMaxUpate").val()) {
                $.OKDialogAgency('<div class="tc">你上传的室内图不能大于' + $("#hidMaxUpate").val() + '张！</div>');
                return false;
            }
        });
    },
    //元素绑定事件
    ElBind: function () {
        //家电按纽
        $("#showjd").toggle(function () {
            $("#appliances").show();
            $(".showsel").hide();
            $(".hidsel").show();
        }, function () {
            $("#appliances").hide();
            $(".showsel").show();
            $(".hidsel").hide();
        });

        //小区共享图库
        $("#btnXiaoQuShareImgs").click(function () {
            var currentElement = $(this);
            currentElement.attr('disabled', 'true');
            var reqData = {
                'estateKeyId': $("#hidEstateKeyId").val()
            };
            $.AjaxAgency('/property/estate-share-imgs-for-estate-key-id-obtain', reqData, function (data) {
                if (data) {
                    $.dialog({
                        id: "xiaoqu-shareMaps",
                        lock: true,
                        title: "小区共享图库",
                        background: "#808080",
                        opacity: 0.87,
                        content: data
                    });
                }
                currentElement.removeAttr('disabled');
            });
        }
        );

        //户型共享图库
        $("#btnRoomShareImgs").click(function () {
            var currentElement = $(this), photoTypeKeyId = '';
            if ($('#uploadRoomTypeDiv').children().length > 0) {
                $.OKDialogAgency('<div class="tc">户型图只能存在一张，如要上传请删除已存在图片！</div>');
                return false;
            }

            currentElement.attr('disabled', 'true');
            for (var i = 0; i < SysParam.length; i++) {
                if (SysParam[i].ParameterType == 56) {
                    photoTypeKeyId = SysParam[i].Items[0].ItemValue;
                    break;
                }
            }
            var reqData = {
                'propertyKeyId': $("#hidPropertyKeyId").val(),
                'photoTypeKeyId': photoTypeKeyId
            };
            $.AjaxAgency('/property/room-share-imgs-obtain', reqData, function (data) {
                if (data) {
                    $.dialog({
                        id: "room-shareMaps",
                        lock: true,
                        title: "户型共享图库",
                        background: "#808080",
                        opacity: 0.87,
                        content: data
                    });
                }
                currentElement.removeAttr('disabled');
            });
        }
        );
    },
    PageLoad: function () {
        this.LoadData();
        this.Verify();
        this.ElBind();
    }
});
//静态方法
//点击保存按纽时,按纽隐藏,这个方法在表单提交时ajax.beginform里调用
realEdit.saveSuceeBegin = function () {
    $('#test-btn-realsafe').css('display', 'none');
    
};
//上传成功后,鼠标经过图片时,设置封面的方法,在图片经过时调用
realEdit.setImgMouseMove = function (element) {
    $(element).find('span.set-face').show();
};
realEdit.setImgMouseOut = function (element) {
    $(element).find('span.set-face').hide();
};
//保存成功调用的方法
realEdit.saveSucee = function (data) {
    var saveResult = '';
    if (data == "no" || data.indexOf("[提示]") > -1 || data == "该户层已存在！") {
        if (data == "该户层已存在！") {
            saveResult = '<div id="realsafe" class="pop-box">' + data + '<div class="tc"><a id="btnGoon" class="btn-big-orange mt20">确定</a></div></div>';
        } else {
            saveResult = '<div id="realsafe" class="pop-box">上传失败！<div class="tc"><a id="btnGoon" class="btn-big-orange mt20">确定</a></div></div>';
        }
        $('#test-btn-realsafe').css('display', '');
    } else {
        saveResult = '<div id="realsafe" class="pop-box">成功上传照片，实勘信息编辑完成！<div class="tc"><a id="btnGoon" class="btn-big-orange mt20" keyId="' + data + '">确定</a></div></div>';
    }

    $.dialog({
        id: "realsafe",
        lock: true,
        close: function () {
            if ($('#btnGoon').attr('keyId')) {
                window.location.href = '/property/property-detail-real-survey-for-keyid-obtain?keyId=' + $('#btnGoon').attr('keyId');
            }
        },
        content: saveResult
    });

    $('#btnGoon').unbind('click');
    $('#btnGoon').click(function () {
        art.dialog.get('realsafe').close();
        if ($(this).attr('keyId')) {
            window.location.href = '/property/property-detail-real-survey-for-keyid-obtain?keyId=' + $(this).attr('keyId');
        }

    });
};
//设置封面
realEdit.setCoverImg = function (element) {
    var tempDefault = $(element).parent().parent().find('input[name=hasDefault]').val();
    var tempNextCover = $(element).next('span');
    if (tempDefault == 'false') {
        $('#RoomImgDiv input[name=hasDefault]').val('false');
        $('#RoomImgDiv span').removeClass('icon-face');
        $(element).parent().parent().find('input[name=hasDefault]').val('true');
        tempNextCover.addClass('icon-face');
    } else {
        $(element).parent().parent().find('input[name=hasDefault]').val('false');
        tempNextCover.removeClass('icon-face');
    }
    realEdit.checkSetCoverImg();
};
//核对封面
realEdit.checkSetCoverImg = function () {
    var tempIsDefault = false;
    $('#RoomImgDiv input[name=hasDefault]').each(function () {
        if ($(this).val() == 'true') {
            tempIsDefault = true;
        }
    });
    if ($("#uploadRoomTypeDiv").children().length > 1) {
        $("#selectFloor").show();
        $("#selectFloor").attr("isnotnull", "1");
    } else {
        $("#selectFloor").hide();
        $("#selectFloor").removeClass("checkError").next(".wrong").remove();
        $("#selectFloor").removeAttr("isnotnull");
        $("#selectFloor").val("");
    }
    if (!tempIsDefault) {
        $($('#RoomImgDiv input[name=hasDefault]')[0]).val('true');
        $($('#RoomImgDiv span[class=isdefault]')[0]).addClass('icon-face');
    }
};
//文本域显示剩余字数
$.inputStatistics("txtRealSurveyRemark", 200);

$.inputStatistics("txtRealSurveyComment", 200);